/*
 *  Copyright (C) 2010 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.balisunrise.nbpam;

import org.openide.modules.ModuleInstall;
import org.openide.windows.Mode;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;

/**
 * Manages a module's lifecycle. Remember that an installer is optional and
 * often not needed at all.
 */
public class Installer extends ModuleInstall {

    static final WindowManager wm = WindowManager.getDefault();

    @Override
    public void restored() {

        wm.invokeWhenUIReady(new Runnable() {

            @Override
            public void run() {

                System.out.println("Start loading the Application\n");
                NBApplicationLoader.startApplication();
                System.out.println("Application loaded.\n");

                wm.updateUI();
                ApplicationTopComponent.findInstance().refresh();
                
            }
        });
    }

    @Override
    public boolean closing() {

        for (Mode mode : wm.getModes()) {

            System.out.println(mode.getName());
            
            for (TopComponent tc : wm.getOpenedTopComponents(mode)) {
                if(tc.canClose())
                    tc.close();
            }
        }
        return true;
    }
}
